/************************************************/
/************************************************/
/**** PART 3: DATA EXTRACTION  ****/
/************************************************/
/************************************************/



/*************************************************/
/*** sample restriction ****/
/*************************************************/

*exclude physicians who see less than 100 patients

	bysort phyid: gen totalpatient=_N
	drop if totalpatient<100  

*exclude visits in which the patient died upon arrival, left before being seen,
* or self-discharged against medical advice

	tab dispositiontype
	
	drop if inlist(dispositiontype,"Death on Arrival Coroner","Death on Arrival NCoroner", ///
	"Left without being seen", "Dis. against Advice") 

*exclude visits with unknown race concordance status
			
    drop if SameRace==. 


*new doctorid

	bysort phyid: gen doctorid=1 if _n==1
	replace doctorid=sum(doctorid)
	label var doctorid "doctor id"



/*************************************************/
/*** extract main variables and ****/
/***  generate analytic data ****/
/*************************************************/

	keep patracegroup Male age agegroup severitylevel severecase ///
	 consultstarthour consultstartdow consultstartmonth ///
	 Admit ADI Consulttime lnConsulttime Revisit Death ImageUse LabUse TestCount SameRace ///
	 doctorid phyracegroup Proportion_Samerace experienced ///
	 detailedICD BrDx SpDx priorcase workhour irelto

	 
	save EDdata,replace
